/*
 * (C) Copyright 2014-2016, by Dimitrios Michail
 *
 * JHeaps Library
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jheaps.array

import org.jheaps.Heap
import org.jheaps.tree.AbstractComparatorLongHeapTest
import org.junit.Assert
import org.junit.Test

class MinMaxBinaryArrayHeapTest : AbstractComparatorLongHeapTest() {
    override fun createHeap(): Heap<Long> {
        return MinMaxBinaryArrayDoubleEndedHeap()
    }

    override fun createHeap(capacity: Int): Heap<Long> {
        return MinMaxBinaryArrayDoubleEndedHeap(capacity)
    }

    override fun createHeap(comparator: Comparator<Long?>?): Heap<Long> {
        return MinMaxBinaryArrayDoubleEndedHeap(comparator)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalSize() {
        val h = createHeap(-4)
        h.insert(1L)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalSize1() {
        val h = createHeap(-1)
        h.insert(1L)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalSize2() {
        val h = createHeap(Int.MAX_VALUE - 8)
        h.insert(1L)
    }

    @Test
    fun testSimple() {
        val h = MinMaxBinaryArrayDoubleEndedHeap<Int>()
        h.insert(1)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(1, h.findMax().toLong())
        h.insert(2)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(2, h.findMax().toLong())
        h.insert(3)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(3, h.findMax().toLong())
        h.insert(10)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(10, h.findMax().toLong())
        h.insert(11)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(11, h.findMax().toLong())
        h.insert(12)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(12, h.findMax().toLong())
        h.insert(5)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(12, h.findMax().toLong())
        h.insert(7)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(12, h.findMax().toLong())
        h.insert(15)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(15, h.findMax().toLong())
        h.insert(100)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(100, h.findMax().toLong())
        h.insert(200)
        Assert.assertEquals(1, h.findMin().toLong())
        Assert.assertEquals(200, h.findMax().toLong())

        Assert.assertEquals(1, h.deleteMin().toLong())

        Assert.assertEquals(2, h.findMin().toLong())
        Assert.assertEquals(200, h.findMax().toLong())
    }

    @Test
    fun testonMinLevel() {
        val h = MinMaxBinaryArrayDoubleEndedHeap<Long>()
        Assert.assertTrue(h.onMinLevel(1))
        Assert.assertFalse(h.onMinLevel(2))
        Assert.assertFalse(h.onMinLevel(3))
        Assert.assertTrue(h.onMinLevel(4))
        Assert.assertTrue(h.onMinLevel(5))
        Assert.assertTrue(h.onMinLevel(6))
        Assert.assertTrue(h.onMinLevel(7))
        for (i in 8..15) {
            Assert.assertFalse(h.onMinLevel(i))
        }
        for (i in 16..31) {
            Assert.assertTrue(h.onMinLevel(i))
        }
        for (i in 32..63) {
            Assert.assertFalse(h.onMinLevel(i))
        }
        for (i in 64..127) {
            Assert.assertTrue(h.onMinLevel(i))
        }
    }
}
